<?php

namespace app\admin\controller;

use app\admin\model\Tap as ModelTap;
use app\BaseController;

class Tap extends Base
{
    public function index()
    {
        return view();
    }
    public function list()
    {
        $param = input();
        $tap = @$param['tap'];
        if (empty($tap)) {
            $list = ModelTap::paginate($param['limit']);
        } else {
            $list = ModelTap::where('tap','like','%' . $tap. '%')->paginate($param);
        }
        return success($list, '操作成功', 0);
    }
    public function update()
    {
        $this->logs('[管理员]更新标签');
        if (request()->isPost()) {
            $param = input();
            $res = ModelTap::update($param);
            if ($res) return success();
            else return error();
        }
    }
    public function add()
    {
        $this->logs('[管理员]添加标签');
        if (request()->isPost()) {
            $param = input();
            $param['is_show'] = isset($param['is_show']) ? 1 : 0;
            $hasTag = ModelTap::where('tap', $param['tap'])->findOrEmpty()->isEmpty();
            if (!$hasTag) return error('已存在该标签');
            $res = ModelTap::create($param);
            if ($res) return success();
            else return error();
        }
    }
    public function del($id)
    {
        $this->logs('[管理员]删除标签');
        $isHas = ModelTap::findOrEmpty($id)->isEmpty();
        if ($isHas) return error("不存在标签[$id]");
        else {
            $res = ModelTap::destroy($id);
            if ($res) return success();
            else return error();
        }
    }
}
